package model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * A class to write and read the HighScore into a file.
 * Places also the new highscore into right position in the list.
 * @author Anton
 */
public class HighScore {

    private static final String file = "score/score.txt";
    private ArrayList<Player> scoreList;


    public HighScore(){
        scoreList = new ArrayList<Player>();
    } //Constructor

    /**
     * Returns the current HighScore list
     * @return current high score list
     */
    public ArrayList<Player> getScoreList(){
        return scoreList;
    } //getScoreList

    /**
     * Inserts the player at correct position in an array.
     * Writes then the new list into the score/score.txt by calling the method wirteToFile
     * @param newPlayer The new player
     * @param newScore The players new score
     */
    public void addNewPlayer(Player newPlayer, int newScore){
        newPlayer.setScore(newScore);
        scoreList.add(newPlayer);
        sortPlayerArray(scoreList);
        writeToFile(scoreList);
    } //addNewPlayerScore


    /**
     * Sorts a list with players after smallest score.
     * @param array The array who should be sorted
     */
    public void sortPlayerArray(ArrayList<Player> array){
        Collections.sort(array, new Comparator<Player>() {

            public int compare(Player x, Player y){
                if(x.getScore() > y.getScore()){
                    return 1;
                }
                if(x.getScore() < y.getScore()){
                    return -1;
                }
                return 0;
            }
        });
    } //sortArray

    /**
     * Writes a ArrayList into the score/score.txt
     * @param list The ArrayList to write
     */
    private void writeToFile(ArrayList<Player> list){
        ObjectOutputStream outputStream = null;
        try{
            //Construct the LineNumberReader object
            outputStream = new ObjectOutputStream(new FileOutputStream(file));
            //Write the object
            outputStream.writeObject(list);

        } catch (FileNotFoundException ex){
            ex.printStackTrace();
        } catch (IOException ex){
            ex.printStackTrace();
        } finally {
            try{
                if(outputStream != null){
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException ex){
                ex.printStackTrace();
            }
        }
    }

    /**
     * A method to show the content in the score file.
     * @return A string representation of the current high score.
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("unchecked")
    public String readFromFile() throws ClassNotFoundException{

        ObjectInputStream inputStream = null;
        Object content = null;

        try{
            //Construct the LineNumberReader object
            inputStream = new ObjectInputStream(new FileInputStream(file));
            //Read the object
            content = inputStream.readObject();

        } catch (FileNotFoundException ex){
            ex.printStackTrace();
        } catch (IOException ex){
            ex.printStackTrace();
        } finally {
            try{
                if(inputStream != null){
                    inputStream.close();
                }
            } catch (IOException ex){
                ex.printStackTrace();
            }
        }

        StringBuffer buf = new StringBuffer();

        //This null-check keeps FindBugs happy
        if(content != null){
            ArrayList<Player> arr = (ArrayList<Player>) content;

            //Turn to nice string representation
            int i = 1;
            buf.append("Spelare:  Antalet slag: \n");
            for(Player aPlayer : arr){
                buf.append(i + ". " + aPlayer.toString());
                i++;
            }
        }

        return buf.toString();
    } //ReadFromFile

} //End of class
